define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ad/adput/index' + location.search,
                    add_url: 'ad/adput/add',
                    edit_url: 'ad/adput/edit',
                    del_url: 'ad/adput/del',
                    multi_url: 'ad/adput/multi',
                    import_url: 'ad/adput/import',
                    table: 'adput',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'adposition.position_name', title: __('Position_id')},
                        {field: 'ad_name', title: __('Ad_name')},
                        {field: 'ad_file', title: __('Ad_file'), operate: false, formatter: Table.api.formatter.images, events: Table.api.events.image},
                        {field: 'mode', title: __('Mode'), searchList: {"0":__('Mode 0'),"1":__('Mode 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'arealist', title: __('Arealist'), searchList: {"0":__('Arealist 0'),"1":__('Arealist 1'),"2":__('Arealist 2'),"3":__('Arealist 3'),"4":__('Arealist 4'),"5":__('Arealist 5'),"6":__('Arealist 6'),"7":__('Arealist 7'),"8":__('Arealist 8'),"9":__('Arealist 9')}, operate:'FIND_IN_SET', formatter: Table.api.formatter.label, operate: false},
                        // {field: 'newhouse_names', title: __('Newhouse_ids'), formatter: Table.api.formatter.label},
                        {field: 'mode', title: __('Putsite'), formatter: Table.api.formatter.label, operate: false, formatter:function(v, d){
                            if (v == 0) {
                                return d.arealist_text;
                            } else if (v == 1) {
                                return d.newhouse_names;
                            }
                        }},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'link_man', title: __('Link_man')},
                        {field: 'admin.nickname', title: __('Admin.nickname')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            var type = $('#c-mode').val()
            // 一进页面就必须初始化一次
            // 普通模式可以不需要
            if(type == 0){
                $('.arealist-class').attr('style','display:block;')
                $('.newhouse-class').attr('style','display:none;')
            }else if(type == 1){
                $('.newhouse-class').attr('style','display:block;')
                $('.arealist-class').attr('style','display:none;')
            }

            $('#c-mode').change(function(){
                var type = $(this).val()
                if(type == 0){
                    $('.arealist-class').attr('style','display:block;')
                    $('.newhouse-class').attr('style','display:none;')
                }else if(type == 1){
                    $('.newhouse-class').attr('style','display:block;')
                    $('.arealist-class').attr('style','display:none;')
                }
            })
        },
        edit: function () {
            Controller.api.bindevent();

            var type = $('#c-mode').val()
            // 一进页面就必须初始化一次
            // 普通模式可以不需要
            if(type == 0){
                $('.arealist-class').attr('style','display:block;')
                $('.newhouse-class').attr('style','display:none;')
            }else if(type == 1){
                $('.newhouse-class').attr('style','display:block;')
                $('.arealist-class').attr('style','display:none;')
            }

            $('#c-mode').change(function(){
                var type = $(this).val()
                if(type == 0){
                    $('.arealist-class').attr('style','display:block;')
                    $('.newhouse-class').attr('style','display:none;')
                }else if(type == 1){
                    $('.newhouse-class').attr('style','display:block;')
                    $('.arealist-class').attr('style','display:none;')
                }
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                var input_id = $('#plupload-ad_file').data("input-id") ? $('#plupload-ad_file').data("input-id") : "";
                var preview_id = $('#plupload-ad_file').data("preview-id") ? $('#plupload-ad_file').data("preview-id") : "";

                if(preview_id) {

                    // 移除按钮事件
                    $(document.body).off("click", "#" + preview_id + " .btn-trash");
                    // 再绑定新按钮事件
                    $(document.body).on("click", "#" + preview_id + " .btn-trash", function () {
                        var that = $(this);
                        // 改成选择当前a标签的上一个元素获取path
                        var path = that.prev().attr('data-url');
                        console.log(path);
                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'ad/adput/delImage',
                                    data: {
                                        id: $("#plupload-ad_file").data('ad_file-id'),
                                        path: path
                                    }
                                }
                                Fast.api.ajax(options, function () {
                                    // 多图片单张删除组合完显示
                                    if (input_id) {
                                        // 获取图片集Value值
                                        var images = $("#" + input_id).val()
                                        // 根据 , 号进行分割数组
                                        var imagesArr = images.split(',')
                                        // 根据path传过来的值进行索引查找 $.inArray 这里用的是JQuery函数
                                        var imageKey = $.inArray(path, imagesArr)
                                        // 删除索引的数组 冲imageKey索引开始，值删除一个值
                                        imagesArr.splice(imageKey, 1)
                                        console.log(imagesArr)
                                        // 重新将数组组合成字符串
                                        var newImagesString = imagesArr.toString()
                                        // 传入input的 value值中
                                        $("#" + input_id).val(newImagesString)
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-ad_file").show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });

                }
            }
        }
    };
    return Controller;
});
